State Monad and StateT Monad
Table of Contents
The State monad represents a computation that receives some state, produces a result, and produces an updated state. It has type State s a that wrappes a function of type s -> (a, s), where a is the result type and s is the state type.
State s a -- => s -> (a, s)
newtype State s a = State { runState :: s -> (a, s) }
So a stateful computation does not mutate a variable directly. Instead, it takes an old state and returns a new state.
1. Main Operations
1.1. State Evaluation
runState returns both the result and the final state; evalState only returns the result; execState only returns the state.
runState :: State s a -> s -> (a, s)